home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Timothy Knox / Pocket6.3 / Extensions / Starting / UsingStartingForth < prev   
Text File  |  1994-06-24  |  13KB  |  169 lines

  1. If you are new to Forth, the best way to learn the language is to use Leo Brodie's book, Starting Forth. Although the book is becoming hard to find, it is still in print (May '93) and can be ordered by your local bookstore. Occasionally you will see it on the shelf of a particularly well stocked large store, or in the public library.
  2.  
  3. Here is the information you will need to obtain a copy for yourself:
  4.  
  5.                       Starting Forth, 2nd ed.
  6.                       by Leo Brodie/FORTH, Inc.
  7.                       Prentice-Hall
  8.                       ISBN 0-13-843079-9
  9.  
  10. For various reasons, Pocket Forth is not fully compatible with Starting Forth. These reasons include keeping size to a minimum, maximizing speed and providing an easy gateway to the Macintosh's unique capabilities. Because of the intent and design philosophy of Pocket Forth, several of the features described in Starting Forth are not implemented at all. You should just skip over those sections of Starting Forth which are identified as being incompatible with Pocket Forth.
  11.  
  12. Load the text file 'Brodie' to use Starting Forth as a text for learning Pocket Forth. The file implements most of the words used in Starting Forth that are not present in Pocket Forth. To load the file: run Pocket Forth, then use the Open... command from Pocket Forth's File menu. Finally select the file 'Brodie'. It will load, then announce its presence.
  13.  
  14. (With System 7, just drag the icon of 'Brodie' to the Pocket Forth icon to load it.) 
  15.  
  16. If you look at the 'Brodie' file, don't worry if some of the definitions do not make sense. Many definitions are written in machine language, which is not very readable.
  17.  
  18. Here is a chapter by chapter discussion of the compatibility issues between the Forth model presented in Brodie's Starting Forth and Pocket Forth.
  19.  
  20.  
  21. Chapter 1
  22. The word SPACES is defined in the file 'Brodie'.
  23.  
  24.  
  25. Chapter 2
  26. The word ".S" is defined in the file 'Brodie'.
  27.  
  28.  
  29. Chapter 3, section 1
  30. Pocket Forth works with the disk differently than does Starting Forth's Forth. Therefore most of the discussion in Chapter 3 does not apply to Pocket Forth. Many modern Forths do not follow Starting Forth, but rather use text files to store source code.
  31.  
  32. Pocket Forth uses regular text files to store source code instead of disk blocks. The function of 'LOAD' can be accessed via the Open selection of the File menu or by dragging a file to Pocket Forth's icon in the Finder (System 7).
  33.  
  34. LIST, TRIAD, THRU, SHOW, PRINT, INDEX and FLUSH are not implemented since they presume disk blocks.
  35.  
  36. Chapter 3, section 2
  37. Pocket Forth has no editor. The line orientation of the Forth editor is very un-Mac-like. A usable editor could be written with Pocket Forth, but auxiliary editors are available, with just about any feature you would like. An adequate editor is TeachText; almost everybody already has it.
  38.  
  39. Start TeachText. Type the source code from page 55. Do not type 'Block# 50' or the line numbers. Be sure to type a return at the end of each line, especially the last line (the one that just has an 'F' on it.) Save the file, name it 'BigF'.
  40.  
  41. Now switch to Pocket Forth. From the File menu, select Open..., find and open the file 'BigF'. You should see the contents of the file scroll down Pocket Forth's window, then a big F made of asterisks will appear. If everything except the big F happens, or you get some garbage characters, be sure the last line ends with a return and that no spaces follow the return. If the message  SPACES ?  appears, be sure to load the file 'Brodie' as described above. Any other errors indicate that you have made a mistake in typing the code, so check each line again.
  42.  
  43. The word .( is defined in the file 'Brodie'.
  44.  
  45.  
  46. Chapter 4
  47. The word ABORT" is not implemented. Instead, use the sequence:
  48.     IF ." Error message" abort THEN
  49.  
  50. The words NOT and <> are defined in the file 'Brodie'.
  51.  
  52.  
  53. Chapter 5
  54. Read the sections about floating point and fractional arithmetic with the following caveat: Pocket Forth supports floating point numbers. As further described in the Chapter 7 discussion below, numbers containing a decimal point or an E are interpreted as floating point numbers. Therefore, replace the word D>F with the word  D>FRAC, defined as follows:
  55.  
  56.     : D>FRAC ( float -- fraction ) f>d drop 1000 /. ;
  57.  
  58. ( NOTE: the word D>F exists in Pocket Forth but with a different meaning. )
  59.  
  60. See footnote on page 111. Omit the ROT.
  61.  
  62. The words 2- and R@ are defined in the file 'Brodie'.
  63.  
  64.  
  65. Chapter 6
  66. The words TRUE , FALSE , I and J are defined in the file 'Brodie'.
  67.  
  68.  
  69. Chapter 7
  70. The word UPPER is defined differently in Pocket Forth. Redefine it as shown on page 146 to do the exercise.
  71.  
  72. Pocket Forth supports double length numbers differently than described in Starting Forth: Numbers entered with a decimal point or other punctuation are interpreted as floating point numbers not double (32 bit) integers. To input double length numbers directly, add an X to the end of the number and do not use a decimal point or other punctuation. Then follow the number with the word F>D. For example:
  73.         123456x f>d d.
  74.  
  75. Philosophical note: Pocket Forth includes extensive floating point arithmetic support. All Macintosh computers include highly accurate floating point math routines in the operating system (it's called SANE, the Standard Apple Numeric Environment.) Most newer Macs include a floating point coprocessor or the option to add one. Thus the floating point support takes little space and (on hardware supported machines) it is very fast. The file 'fvgFloatingPoint' supports the ad hoc Forth Vendors Group floating point math standard.
  76.  
  77. The word SIGN takes its flag from the third item on the stack. In reference to the footnote on page 155, Pocket Forth uses the old model.
  78.  
  79. The following words are defined in the file 'Brodie': OCTAL  ASCII  D-  DMAX  DMIN  D=  D<  U<  UM*  UM/MOD  M*  M+  M/ M*/  .R  D.R  U.R
  80.  
  81.  
  82. Chapter 8
  83. Some instructions for the microprocessor that powers the Mac require that addressing be done on even memory addresses only. Because of this, arguments to  @  !  and  ALLOT  must be even numbers.
  84.  
  85. Ordinarily you do not need to worry about this. Addresses created with Pocket Forth are always even unless specifically made odd by adding an odd number to the address.
  86.  
  87. The word  0. is not implemented. See the Chapter 5 and 7 discussions above about numbers with decimal points.
  88.  
  89. You may implement  DUMP  as shown in Starting Forth. Another definition is given in the text file 'Misc'.
  90.  
  91. The words ?  ERASE  BL  BLANK and C, are defined in the file 'Brodie'.
  92.  
  93.  
  94. Chapter 9
  95. This chapter deals with the make up of the language. Pocket Forth differs to a large extent in its composition and structure. The main difference is the way words are compiled. In Starting Forth, words are compiled as a sixteen bit address. Pocket Forth on the other hand compiles the actual machine instructions needed to execute the word, using what is known as subroutine threading.
  96.  
  97. Fill in the table (Computing "EXECUTE address") on page 197 with the following:
  98.      Forth version:    Pocket Forth
  99.             column 1:    ' name
  100.             column 2:    '
  101.             column 3:    ['] name                  ( if 'Brodie' is loaded.)
  102.                                [ ' name  literal ]   ( if 'Brodie' is not loaded.)
  103.        tick returns:    code field
  104.  
  105. All Pocket Forth words have name and link fields as shown in Starting Forth. Instead of a code pointer, the code itself follows the link field. This is referred to as the code (not code POINTER) field. The instructions in the code field are unique to the definition they are part of. This is different than Starting Forth, where every colon definition has the same code pointer, and only the parameter field entries are unique.
  106.  
  107. Only words defined with CREATE , VARIABLE , 2VARIABLE or FVARIABLE have a parameter field. The parameter field follows the code field. Since the length of the code field is indeterminant, do not attempt to calculate the parameter field address from the tick address. Words with parameter fields return their parameter field address. The word >BODY is not defined.
  108.  
  109.  
  110. Recursion in Pocket Forth is straight forward. The word RECURSE is not needed, but it is defined in the 'Brodie' file. Newly defined word names are not hidden during compilation, so any definition can call itself. The process to redefine a word by calling itself is somewhat harder, though. See the definition of  WORD  in the file 'Brodie' for an example of how to do this.
  111.  
  112.  
  113.  
  114. The memory map of Pocket Forth is similar to but different from the one shown in Starting Forth.  Refer to the Pocket Forth manual for more information.
  115.  
  116.  
  117. Pocket Forth does not implement vocabularies.  Pocket Forth is meant for small programming tasks, and the Macintosh operating system allows for multiple programs to run simultaneously, thus fulfilling some of the need for vocabularies.  Pocket Forth's dictionary is linear and non-branching.
  118.  
  119. Pocket Forth's stacks are not in the dictionary.  The words S0  and SP@  yield absolute addresses as results.  See the Pocket Forth manual for a discussion of address types.
  120.  
  121. These words are not implemented:  >BODY  DP  'S  VOCABULARY  FORTH  EDITOR  ASSEMBLER  CONTEXT  CURRENT  and DEFINITIONS.
  122.  
  123. These words are defined in the file 'Brodie': [']  @EXECUTE  RECURSE  H  and  S0 .
  124.  
  125.  
  126. Chapter 10
  127. Here we go again. Pocket Forth has no BLOCKS, no block BUFFERS and none of the block support words (see below.)  The use of text files for loading source code is covered in the Pocket Forth manual. Files used for data are demonstrated in the files 'DataFiles' and 'TextEdit'.
  128.  
  129. Because it relies so heavily on BLOCKs and BUFFERS, do not do the buzzword demo. The file 'Buzz' (in the Chapter12 folder) provides an alternative way of getting the same result.
  130.  
  131. These words are not implemented:  UPDATE  SAVE-BUFFERS  FLUSH  EMPTY-BUFFERS  BLOCK  BUFFER  CONVERT  >TYPE  -TEXT and -MATCH.
  132.  
  133. These words are defined in the file 'Brodie':  KEY?  MOVE  CMOVE>  SPAN  >IN  BLK  STRING  LIT".
  134.  
  135. Pocket Forth's WORD is redefined as  WORD HERE  and  NUMBER  is redefined to return a double length number, however, the largest number converted is 65535.  Pocket Forth's  NUMBER  returns a single number and a flag indicating success in the conversion. See the note below.
  136.  
  137.  
  138. Chapter 11
  139. The word  STATE  is defined in the file 'Brodie'.
  140.  
  141. COMPILE is redefined.  Pocket Forth's COMPILE is quite a bit different from the COMPILE defined in Starting Forth.  To get
  142. the same result without loading the 'Brodie' file, compare the
  143. following to the example on pages 271 and 272:
  144.  
  145.     : TEST  [ ' beep literal ] compile ; IMMEDIATE
  146.     : 2CRS  cr  test  cr ;  \ silence
  147.     2crs                    \ a beep is heard
  148.  
  149. (Of course, the example in Starting Forth works without this modification if the file 'Brodie' is loaded.)
  150.  
  151. NOTE:  Since no additional utility is provided by the three redefined words, you may remove the redefinitions by entering the line:
  152.       forget word  : task ;
  153.  
  154.  
  155. Chapter 12
  156. The folder Chapter 12 contains two of the example programs from Starting Forth's last chapter. The file 'Buzz' does the buzzword demo while 'NoWeighting' does the material measuring calculator.
  157.  
  158. The File Away and Assembler demos are not recreated. The filing application must have extensive modifications due to its reliance on Forth's BLOCK and BUFFER disk access structure. It would be a good exercise to write your own simple filer using a data file. See the file 'DataFiles' in the Examples folder for some ideas on using files.
  159.  
  160. The Assembler demo will work with minor changes to eliminate the vocabulary use and account for the differences in Pocket Forth's structure, but an 8080 assembler is not very useful on a Macintosh. A 68000 assembler in the same style has been presented in Dr. Dobbs Journal (September, 1983) which you can modify to work. See the Pocket Forth manual for information on machine language programming.
  161.  
  162.  
  163. Conclusion
  164. Pocket Forth can be used for almost any task where Forth would be the chosen language.  It is an idiosyncratic version of the language though, designed for efficiency, not compatibility. By loading the 'Brodie' file, most of the examples from Starting Forth and much published code can be run with only minor modification. Note, however, equivalent results can often be obtained with 'native' Pocket Forth.
  165.  
  166. NOTE: None of the example files outside of the Learning folder require 'Brodie' to be loaded. Because of word redefinitions, some examples may not run correctly if it is.
  167.  
  168. Pocket Forth now gives you the choice of compatibility vs. efficiency. Good luck on your programming adventures.
  169.